//
//  HYSalonViewController.m
//  KaiJing
//
//  Created by lanou on 16/1/14.
//  Copyright © 2016年 HuangZuCui. All rights reserved.
//

#import "HYSalonViewController.h"
#import "PKRequestManager.h"
#import "HYSalonModel.h"
#import <UIImageView+WebCache.h>
#import "HYSalonHeaderView.h"
#import <UIButton+WebCache.h>
#import "HYSalonDetaController.h"
@interface HYSalonViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, strong) HYSalonHeaderView *headerView;

@end

static NSString *identifier = @"HYSalonViewController";
@implementation HYSalonViewController


//沙龙
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpData];
}

- (void) setUpData {
    [PKRequestManager requestWithUrlString:@"http://morguo.com/forum.php?mod=collection&op=channel&androidflag=1&appfrom=ios&iosversion=3.13&page=1" parDic:nil method:GET finish:^(NSDictionary *dataDic) {
        
        NSArray *liatArray = dataDic[@"data"][@"list"];
        _dataArray = [NSMutableArray array];
        for (NSDictionary *dic in liatArray) {
            HYSalonModel *model = [[HYSalonModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [_dataArray addObject:model];
        }
        [self addTableView];
        [_tableView reloadData];
    } error:^(NSError *error) {
        
    }];
    
}

- (void)addTableView {
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 113) style:(UITableViewStylePlain)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    //_tableView.rowHeight = 70;
    _headerView = [[HYSalonHeaderView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight / 4)];
    _headerView.backgroundColor = kRandomClolor;
    _tableView.tableHeaderView = _headerView;
    _headerView.array = _dataArray;
    
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identifier];
    }
    
    
        HYSalonModel *model = _dataArray[indexPath.row];
   
    if (indexPath.row < 2) {
        cell.textLabel.text = nil;
        cell.detailTextLabel.text = nil;
    }
        cell.textLabel.text = model.name;
        NSString *threadnum = [NSString stringWithFormat:@"已有%@人参加",model.threadnum];
        cell.detailTextLabel.text = threadnum;
        //cell.imageView.backgroundColor = kRandomClolor;
       [cell.imageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://morguo.com/%@",model.icon]] placeholderImage:[UIImage imageNamed:@"menu_pictorial"]];
    
    return cell;
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row <2) {
        return 0;
    }else {
        return 70;
    }
}


-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    HYSalonDetaController *detaVc = [[HYSalonDetaController alloc] init];
    HYSalonModel *model = _dataArray[indexPath.row];
    detaVc.model = model;
    
    [self.navigationController pushViewController:detaVc animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
